#include "SceneImporter.h";

SceneImporter::SceneImporter(void)
{
	loadOkay = false;
	nCurrentSeq = 99;
}

//-----------------------------------------------------------

SceneImporter::~SceneImporter(void)
{
	clearSaveFiles();
}

//----------------------------------------------------------

void SceneImporter::importScene(PredefinedCameraDescriptions *mPredefinedCameraDescriptions, Ogre::SceneManager *mSceneMgr, Map *mMap, WeatherSystem *mWeatherSystem)
{
	std::cout << "\n<SceneImporter>: building scene...";

	if (loadOkay)
	{
		std::cout << "\n<SceneImporter>: file loaded, processing...";

		TiXmlElement *root = sourceFile.FirstChildElement();

		if(root != 0)
		{
			TiXmlElement *currentNode;
			Ogre::String nodeId;
			Ogre::String tempValue;

			currentNode = root->FirstChildElement();

			while(currentNode != 0)
			{
				nodeId = currentNode->Value();


					if(nodeId == "CameraDescriptions")
					{	

						std::cout << "\n<SceneImporter>: Building camera list...";

						TiXmlElement *currentDescription;
						Ogre::String camString = "";
						Ogre::Vector3 camPosition;
						Ogre::Quaternion camOrientation;

						currentDescription = currentNode->FirstChildElement();

						while(currentDescription != 0)
						{


										TiXmlElement *currentProperty;
										Ogre::String propertyId;

										currentProperty = currentDescription->FirstChildElement();

										while(currentProperty != 0)
										{
											propertyId = currentProperty->Value();

											if(propertyId == "CameraString")
											{
												tempValue = currentProperty->GetText();
												camString = tempValue;

											}
											else if(propertyId == "CameraPoz")
											{
												tempValue = currentProperty->GetText();
												camPosition = Ogre::StringConverter::parseVector3(tempValue);
											}
											else if(propertyId == "CameraRot")
											{
												tempValue = currentProperty->GetText();
												camOrientation = Ogre::StringConverter::parseQuaternion(tempValue);
											}


											currentProperty = currentProperty->NextSiblingElement();
										}

										mPredefinedCameraDescriptions->saveCamera(camString,camPosition,camOrientation);

							currentDescription = currentDescription->NextSiblingElement();
						}

					}//</CameraDescriptions>
					else if(nodeId == "MapData")
					{
						std::cout << "\n<SceneImporter>: Building map...";

						Ogre:String heightMap = "";

										TiXmlElement *currentProperty;
										Ogre::String propertyId;

										currentProperty = currentNode->FirstChildElement();

										while(currentProperty != 0)
										{
											propertyId = currentProperty->Value();

											if(propertyId == "HeightMap")
											{
												tempValue = currentProperty->GetText();
												heightMap = tempValue;

											}


											currentProperty = currentProperty->NextSiblingElement();
										}

										mMap->rebuildTerrain(heightMap);
					}//</MapData>
					else if(nodeId == "WeatherData")
					{
						std::cout << "\n<SceneImporter>: Setting the weather...";

						WeatherSystem::eDayTime dayTime;
						bool isRaining;
						bool isSnowing;
						int snowLevel;

										TiXmlElement *currentProperty;
										Ogre::String propertyId;

										currentProperty = currentNode->FirstChildElement();

										while(currentProperty != 0)
										{
											propertyId = currentProperty->Value();

											if(propertyId == "DayTime")
											{
												tempValue = currentProperty->GetText();

												if(tempValue == "Dawn")
												{
													dayTime = WeatherSystem::DayTime_Dawn;
												}
												else if(tempValue == "Noon")
												{
													dayTime = WeatherSystem::DayTime_Noon;
												}
												else if(tempValue == "AfterNoon")
												{
													dayTime = WeatherSystem::DayTime_AfterNoon;
												}
												else if(tempValue == "SunSet")
												{
													dayTime = WeatherSystem::DayTime_SunSet;
												}
												else if(tempValue == "Night")
												{
													dayTime = WeatherSystem::DayTime_Night;
												}
											}
											else if(propertyId == "isRaining")
											{
												tempValue = currentProperty->GetText();
												isRaining = Ogre::StringConverter::parseBool(tempValue);
											}
											else if(propertyId == "isSnowing")
											{
												tempValue = currentProperty->GetText();
												isSnowing = Ogre::StringConverter::parseBool(tempValue);
											}
											else if(propertyId == "SnowLevel")
											{
												tempValue = currentProperty->GetText();
												snowLevel = Ogre::StringConverter::parseInt(tempValue);
											}

											currentProperty = currentProperty->NextSiblingElement();
										}


								mWeatherSystem->setDayTime(dayTime);
								if(isSnowing)
								{
									mWeatherSystem->createSnow();
								}
								if(isRaining)
								{
									mWeatherSystem->createRain();
								}
								mWeatherSystem->setSnowLevel(snowLevel);
					}//</WeatherData>


				currentNode = currentNode->NextSiblingElement();
			}
		}
	}
}

//----------------------------------------------------------

void SceneImporter::importActors(Critter::RenderSystem *mRenderSystem,  Ogre::SceneManager *mSceneMgr, NxOgre::Scene *mScene, PhysXWorld *mPhysXWorld)
{
	std::cout << "\n<SceneImporter>: building actors...";

	if (loadOkay)
	{
		std::cout << "\n<SceneImporter>: file loaded, processing...";

		TiXmlElement *root = sourceFile.FirstChildElement();

		if(root != 0)
		{
			TiXmlElement *currentNode;
			Ogre::String nodeId;
			Ogre::String tempValue;

			currentNode = root->FirstChildElement();

			while(currentNode != 0)
			{
				nodeId = currentNode->Value();


					if(nodeId == "ObjectData")
					{
								TiXmlElement *currentActor;
								Ogre::String actorName = "";
								WorldEnums::eSceneryObjects actorType;
								Ogre::Vector3 actorPoz;
								Ogre::Vector3 actorScale;
								Ogre::Quaternion actorRot;

								currentActor = currentNode->FirstChildElement();

								while(currentActor != 0)
								{

									std::cout << "\n<SceneImporter>: building actor...";	
									//-------------------------------------------
										TiXmlElement *currentProperty;
										Ogre::String propertyId;

										currentProperty = currentActor->FirstChildElement();

										while(currentProperty != 0)
										{
											propertyId = currentProperty->Value();

											if(propertyId == "Name")
											{
												tempValue = currentProperty->GetText();
												actorName = tempValue;

											}
											else if(propertyId == "Type")
											{
												tempValue = currentProperty->GetText();

												if(tempValue == "Block1")
												{
													actorType = WorldEnums::Scenery_Block1;
												}
												if(tempValue == "Block2")
												{
													actorType = WorldEnums::Scenery_Block2;
												}
												if(tempValue == "Block3")
												{
													actorType = WorldEnums::Scenery_Block3;
												}
												if(tempValue == "House1")
												{
													actorType = WorldEnums::Scenery_House1;
												}
												if(tempValue == "House2")
												{
													actorType = WorldEnums::Scenery_House2;
												}
												if(tempValue == "Tree1")
												{
													actorType = WorldEnums::Scenery_Tree1;
												}
												if(tempValue == "Tree2")
												{
													actorType = WorldEnums::Scenery_Tree2;
												}
												if(tempValue == "Lake1")
												{
													actorType = WorldEnums::Scenery_Lake1;
												}
												if(tempValue == "Lake2")
												{
													actorType = WorldEnums::Scenery_Lake2;
												}
												if(tempValue == "Lake3")
												{
													actorType = WorldEnums::Scenery_Lake3;
												}

											}
											else if(propertyId == "ActorPoz")
											{
												tempValue = currentProperty->GetText();
												actorPoz = Ogre::StringConverter::parseVector3(tempValue);
											}
											else if(propertyId == "ActorRot")
											{
												tempValue = currentProperty->GetText();
												actorRot = Ogre::StringConverter::parseQuaternion(tempValue);
											}
											else if(propertyId == "ActorScale")
											{
												tempValue = currentProperty->GetText();
												actorScale = Ogre::StringConverter::parseVector3(tempValue);
											}


											currentProperty = currentProperty->NextSiblingElement();
										}
									//-------------------------------------------


									mPhysXWorld->createScenery(actorType,NxOgre::Vec3(actorPoz.x,actorPoz.y,actorPoz.z),mScene,mSceneMgr);
									int actorIndex = mPhysXWorld->lActors->size() - 1;
									mPhysXWorld->setSceneryObjectScale(actorIndex, actorScale);
									mPhysXWorld->setSceneryObjectRotation(actorIndex, actorRot);
									
									//attaching emitters, setting capacity and level if this is a holder actor
									if(mPhysXWorld->isHolderActor(actorIndex))
									{
										Lake *lake = (Lake*)(mPhysXWorld->lActors->at(actorIndex));
										int capacity = 0;
										int level = 0;

											currentProperty = currentActor->FirstChildElement();
											while(currentProperty != 0)
											{
												if(propertyId == "ActorCapacity")
												{
													tempValue = currentProperty->GetText();
													capacity = Ogre::StringConverter::parseInt(tempValue);
													lake->setCapacity(capacity);
												}
												else if(propertyId == "ActorLevel")
												{
													tempValue = currentProperty->GetText();
													level = Ogre::StringConverter::parseInt(tempValue);
													lake->setCurrentLevel(0);
													lake->incLevel(level);
												}
												else if(propertyId == "AttachedEmitters")
												{
													TiXmlElement *currentEmitter;
													Ogre::String emitterName = "";
													Ogre::Vector3 emitterPoz;

													currentEmitter = currentProperty->FirstChildElement();
													while(currentEmitter != 0)
													{

														TiXmlElement *nameNode = currentEmitter->FirstChildElement("Name");
														TiXmlElement *pozNode = currentEmitter->FirstChildElement("Poz");

														if(nameNode!=0)
														{
															tempValue = nameNode->GetText();
															emitterName = tempValue;
														}

														if(pozNode!=0)
														{
															tempValue = pozNode->GetText();
															emitterPoz = Ogre::StringConverter::parseVector3(tempValue);
														}

												
														mPhysXWorld->attachWaterEmitter(lake, mRenderSystem, mSceneMgr, emitterPoz.x, emitterPoz.y, emitterPoz.z);
												

														currentEmitter = currentEmitter->NextSiblingElement();
													}


												}

												currentProperty = currentProperty->NextSiblingElement();
											}
										
									}

									currentActor = currentActor->NextSiblingElement();
								}
					}


				currentNode = currentNode->NextSiblingElement();
			}
		}
	}
}

//--------------------------------------------------------------------------

void SceneImporter::clearSaveFiles(void)
{
	int count = saveFiles.size();

	for(int i=0;i<count;i++)
	{
		delete saveFiles.at(i);
	}
	saveFiles.clear();
}



//----------------------------------------------------------

void SceneImporter::importSaveDataFromXmlFile(Ogre::String fileName)
{
	std::cout << "\n<SceneImporter>: loading data from file: " << fileName.data();

	loadOkay = sourceFile.LoadFile(fileName.data());
}

//----------------------------------------------------------------------------

void SceneImporter::importSaveDataFromXmlString(Ogre::String dataString)
{
	std::cout << "\n<SceneImporter>: loading data from string...";
	sourceFile.Clear();
	sourceFile.Parse(dataString.data());

	TiXmlElement *root = sourceFile.FirstChildElement();

	if(root==0)
	{
		loadOkay = false;
		std::cout << "\n<SceneImporter>: Parse error!";
	}
	else
	{
		loadOkay = true;
		std::cout << "\n<SceneImporter>: datastring parsed!";
	}
}


//----------------------------------------------------------------------------

void SceneImporter::importSceneDBFromXml(Ogre::String fileName)
{
	clearSaveFiles();

    std::cout << "\n<SceneImporter>: building save file list from file: " << fileName.data();

	bool loadOkay = sourceFile.LoadFile(fileName.data());

	if (loadOkay)
	{
		std::cout << "\n<SceneImporter>: file loaded, processing...";

		TiXmlElement *root = sourceFile.FirstChildElement();

		if(root != 0)
		{
			TiXmlElement *seqNode = root->FirstChildElement("seq");

			if(seqNode != 0)
			{
				nCurrentSeq = Ogre::StringConverter::parseInt(seqNode->GetText());
			}

			TiXmlElement *saveFileNode = root->FirstChildElement("SaveFile");

			while(saveFileNode != 0)
			{
				TiXmlElement *nameNode = saveFileNode->FirstChildElement("Name");
				TiXmlElement *descriptionNode = saveFileNode->FirstChildElement("Description");
				TiXmlElement *fileNameNode = saveFileNode->FirstChildElement("FileName");

				Ogre::String sName = "";
				Ogre::String sDescription = "";
				Ogre::String sFileName = "";

				if(nameNode != 0)
				{
					sName = nameNode->GetText();
				}

				if(descriptionNode != 0)
				{
					sDescription = descriptionNode->GetText();
				}

				if(fileNameNode != 0)
				{
					sFileName = fileNameNode->GetText();
				}

				saveFiles.push_back(new SaveFileDescription(sName,sFileName,sDescription));

				saveFileNode = saveFileNode->NextSiblingElement();
			}
		}
	}
}

//----------------------------------------------------------------------------